﻿using IService;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Microsoft.EntityFrameworkCore;

namespace Service
{
    public class UsersService : IUsersService
    {
        private UsersDal _u;
        private readonly AppDbContext _dbContext;
        public UsersService(UsersDal u, AppDbContext dbContext)
        {
            _u = u;
            _dbContext = dbContext;
        }

        public Task<Users> CreateAsync(Users data)
        {
            return _u.CreateAsync(data);
        }

        public Task<int> DeleteAsync(int Id)
        {
            return _u.DeleteAsync(Id);
        }

        public Task<List<Users>> GetAllAsync()
        {
            return _u.GetAllAsync();
        }

        public Task<Users> GetByIdAsync(int Id)
        {
            return _u.GetByIdAsync(Id);
        }

        public Task<int> UpdateAsync(Users date)
        {
            return _u.UpdateAsync(date);
        }

        // 实现根据部门ID查询用户的方法（关键！）
        public async Task<List<Users>> GetUsersByOrgIdAsync(long orgId)
        {
            // 从数据库查询指定部门的用户（OrgId 是用户表中关联部门的字段）
            return await _dbContext.Users
                .Where(u => u.OrgId == orgId) // 筛选条件：用户的部门ID等于传入的orgId
                .ToListAsync();
        }
    }
}
